/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSArrayController.h"

IMPLEMENT_DYNAMIC_CLASS(NSArrayController, NSObjectController)

NSArrayController::NSArrayController() : NSObjectController()
{
	_indexes = new NSMutableArray();
	disableAutomaticObservation(wxT("selectedObjects")); // no need, it will be generated during setSelectionIndexes
	disableAutomaticObservation(wxT("canRemove")); // depends on and will be updated with contentArray
	exposeBinding(wxT("contentArray"));
	exposeBinding(wxT("canRemove"));
}

void NSArrayController::_setValue(const wxString& key, wxObject* value)
{
	if (key == wxT("contentArray"))
	{
		_willValueChangedForKey(wxT("canRemove"));
		_willValueChangedForKey(wxT("arrangedObjects"));
		NSObjectController::setValue(wxT("content"), value);
		_didValueChangedForKey(wxT("selectedObjects"));
		_didValueChangedForKey(wxT("arrangedObjects"));
		_didValueChangedForKey(wxT("canRemove"));
	} else if (key == wxT("arrangedObjects"))
	{
		if (value)
		{
			_willValueChangedForKey(wxT("canRemove"));
			_willValueChangedForKey(wxT("contentArray"));
			_willValueChangedForKey(wxT("selectedObjects"));
			NSObjectController::setValue(wxT("content"), value);
			_didValueChangedForKey(wxT("selectedObjects"));
			_didValueChangedForKey(wxT("contentArray"));
			_didValueChangedForKey(wxT("canRemove"));
		}
	} else if (key == wxT("selectionIndexes"))
	{
		NSMutableArray* ind = wxDynamicCast(value, NSMutableArray);
		wxASSERT(ind);
		_setIndexes(ind);
	} else if (key == wxT("selectedObjects"))
	{
		NSMutableArray *obj = wxDynamicCast(value, NSMutableArray);
		wxASSERT(obj);
		_generateIndexes(obj);
	} else
		NSObjectController::_setValue(key, value);
}

wxObject* NSArrayController::_getValue(const wxString& key)
{
	if (key == wxT("contentArray") || key == wxT("arrangedObjects"))
	{
		return NSObjectController::getValue(wxT("content"));
	} else if (key == wxT("selectedObjects"))
	{
		return _generateObjects();
	} else if (key == wxT("canRemove"))
	{
		wxObject* obj = getValue(wxT("contentArray"));
		if (!obj)
			return new wxVariant(false);
		NSMutableArray *arr = wxDynamicCast(obj, NSMutableArray);
		if (!arr)
			return new wxVariant(false);
		return new wxVariant(arr->length() > 0);
	} else if (key == wxT("selectionIndexes"))
	{
		return _indexes;
	} else
		return NSObjectController::_getValue(key);
}

wxObject* NSArrayController::_generateObjects()
{
	NSMutableArray* arr = new NSMutableArray();
	NSMutableArray* w_arr = wxDynamicCast(getValue(wxT("contentArray")), NSMutableArray);
	if (w_arr) for(size_t i=0;i<_indexes->length();i++)
	{
		wxVariant* v = wxDynamicCast(_indexes->getObject(i), wxVariant);
		wxASSERT(v);
		size_t pos = v->GetLong();
		if (w_arr->length()>pos)
			arr->addObject(w_arr->getObject(pos));
	}
	return arr;
}

void NSArrayController::_generateIndexes(NSMutableArray* objs)
{
	NSMutableArray* arr = new NSMutableArray();
	NSMutableArray* w_arr = wxDynamicCast(getValue(wxT("contentArray")), NSMutableArray);
	if (w_arr) for(size_t i=0;i<objs->length();i++)
	{
		wxObject* obj = objs->getObject(i);
		for(size_t j=0;j<w_arr->length();j++)
			if (w_arr->getObject(j) == obj)
			{
				arr->addObject(new wxVariant((long)j));
				break;
			}
	}
	_setIndexes(arr);
}

void NSArrayController::_setIndexes(NSMutableArray* ind)
{
	_willValueChangedForKey(wxT("selectedObjects"));
	_indexes = ind;
	_didValueChangedForKey(wxT("selectedObjects"));
}
